package com.tengju.bff.interfaces.covercharge.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tengju.bff.interfaces.covercharge.constant.RewardPunishCategoryStatusEnum;
import com.tengju.bff.interfaces.covercharge.entity.po.ServiceRewardPunishCategory;
import com.tengju.bff.interfaces.covercharge.entity.vo.AdminCategoryPageQuery;
import com.tengju.bff.interfaces.covercharge.entity.vo.AdminCategoryQuery;
import com.tengju.bff.interfaces.covercharge.entity.vo.AdminCategoryVo;
import com.tengju.bff.interfaces.covercharge.entity.vo.AdminPunishApplyVo;
import com.tengju.bff.interfaces.covercharge.mapper.ServiceRewardPunishCategoryMapper;
import com.tengju.bff.interfaces.covercharge.service.IServiceRewardPunishCategoryService;
import com.zoi7.component.core.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 奖惩类目表 服务实现类
 * </p>
 *
 * @author zhaoyi
 * @since 2020-12-16
 */
@Slf4j
@Service
public class ServiceRewardPunishCategoryServiceImpl extends ServiceImpl<ServiceRewardPunishCategoryMapper, ServiceRewardPunishCategory> implements IServiceRewardPunishCategoryService {

    /**
     * 根据天健获取类目信息
     *
     * @param adminCategoryPageQuery 类目请求Query
     * @return 查询结果
     */
    @Override
    public IPage<AdminCategoryVo> getCategoryInfo(AdminCategoryPageQuery adminCategoryPageQuery) {
        QueryWrapper<ServiceRewardPunishCategory> queryWrapper = new QueryWrapper<>();
        if (adminCategoryPageQuery.getParentId() == null) {
            //为空表示页面加载查询,条件为只查子类目
            queryWrapper.ne("parent_id", 0);
        } else {
            //按条件查询
            queryWrapper.eq("parent_id", adminCategoryPageQuery.getParentId());
        }
        queryWrapper.eq(adminCategoryPageQuery.getStatus() != null, "status", adminCategoryPageQuery.getStatus());
        int pageNo = adminCategoryPageQuery.getCurrent();
        int limit = adminCategoryPageQuery.getSize();
        queryWrapper.orderByDesc("created_time");
        IPage<ServiceRewardPunishCategory> page = this.page(new Page<>(pageNo, limit), queryWrapper);
        List<AdminCategoryVo> resultList = new ArrayList<>();
        for (ServiceRewardPunishCategory serviceRewardPunishCategory : page.getRecords()) {
            AdminCategoryVo adminCategoryVo = new AdminCategoryVo();
            //查找是否为子类目
            adminCategoryVo.setId(serviceRewardPunishCategory.getId());
            adminCategoryVo.setParentId(serviceRewardPunishCategory.getParentId());
            adminCategoryVo.setTitle(serviceRewardPunishCategory.getTitle());
            adminCategoryVo.setStatus(serviceRewardPunishCategory.getStatus());
            if (adminCategoryVo.getParentId() != null && adminCategoryVo.getParentId() != 0) {
                //查找此子类目的父类目
                QueryWrapper<ServiceRewardPunishCategory> parentQuery = new QueryWrapper<>();
                parentQuery.eq("id", serviceRewardPunishCategory.getParentId());
                ServiceRewardPunishCategory parentCategory = this.getOne(parentQuery);
                if (parentCategory != null) {
                    adminCategoryVo.setParentTitle(parentCategory.getTitle());
                }
            }
            resultList.add(adminCategoryVo);
        }
        IPage<AdminCategoryVo> pageResult = new Page<>();
        pageResult.setRecords(resultList);
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    /**
     * 更改类目状态
     *
     * @param id      类目请求Vo
     * @param status  新状态
     * @param operate 操作人
     */
    @Override
    public void updateCategoryStatus(int id, int status, String operate) {
        UpdateWrapper<ServiceRewardPunishCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        ServiceRewardPunishCategory serviceRewardPunishCategory = new ServiceRewardPunishCategory();
        serviceRewardPunishCategory.setId(id);
        serviceRewardPunishCategory.setStatus(status);
        serviceRewardPunishCategory.setUpdateBy(operate);
        serviceRewardPunishCategory.setUpdatedTime(LocalDateTime.now());
        this.update(serviceRewardPunishCategory, updateWrapper);
    }

    /**
     * 新增类目
     *
     * @param adminCategoryVo 新增实体
     * @param operate         操作人
     */
    @Override
    public void insertCategory(AdminCategoryVo adminCategoryVo, String operate) {
        //1、校验原类目是否存在
        QueryWrapper<ServiceRewardPunishCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        queryWrapper.eq("title", adminCategoryVo.getTitle());
        List<ServiceRewardPunishCategory> list = this.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            throw new ServiceException("该类目已存在!");
        }
        //2、新增
        ServiceRewardPunishCategory serviceRewardPunishCategory = new ServiceRewardPunishCategory();
        serviceRewardPunishCategory.setTitle(adminCategoryVo.getTitle());
        //父类目parent_id为0
        serviceRewardPunishCategory.setParentId(0);
        serviceRewardPunishCategory.setStatus(RewardPunishCategoryStatusEnum.ENABLE.getStatus());
        serviceRewardPunishCategory.setCreateBy(operate);
        serviceRewardPunishCategory.setCreatedTime(LocalDateTime.now());
        this.save(serviceRewardPunishCategory);
    }

    /**
     * 新增类目
     *
     * @param adminCategoryVo 新增实体
     * @param operate         操作人
     */
    @Override
    public void insertCategoryValue(AdminCategoryVo adminCategoryVo, String operate) {
        //1、校验原类目是否存在
        QueryWrapper<ServiceRewardPunishCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", adminCategoryVo.getTitle());
        queryWrapper.eq("parent_id", adminCategoryVo.getParentId());
        List<ServiceRewardPunishCategory> list = this.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            throw new ServiceException("该类目值已存在!");
        }
        //2、新增
        ServiceRewardPunishCategory serviceRewardPunishCategory = new ServiceRewardPunishCategory();
        serviceRewardPunishCategory.setTitle(adminCategoryVo.getTitle());
        serviceRewardPunishCategory.setParentId(adminCategoryVo.getParentId());
        serviceRewardPunishCategory.setStatus(1);
        serviceRewardPunishCategory.setCreateBy(operate);
        serviceRewardPunishCategory.setCreatedTime(LocalDateTime.now());
        this.save(serviceRewardPunishCategory);
    }

    /**
     * 获取类目信息
     *
     * @param adminCategoryQuery 类目请求Query
     * @return 查询结果
     */
    @Override
    public List<AdminCategoryVo> getCategoryInfoList(AdminCategoryQuery adminCategoryQuery) {
        QueryWrapper<ServiceRewardPunishCategory> queryWrapper = new QueryWrapper<>();
        if (adminCategoryQuery != null) {
            queryWrapper.eq(adminCategoryQuery.getParentId() != null, "parent_id", adminCategoryQuery.getParentId());
            queryWrapper.eq(adminCategoryQuery.getStatus() != null, "status", adminCategoryQuery.getStatus());
        }
        List<ServiceRewardPunishCategory> resultList = this.list(queryWrapper);
        List<AdminCategoryVo> result = new ArrayList<>();
        for (ServiceRewardPunishCategory serviceRewardPunishCategory : resultList) {
            AdminCategoryVo adminCategoryVo = new AdminCategoryVo();
            adminCategoryVo.setId(serviceRewardPunishCategory.getId());
            adminCategoryVo.setStatus(serviceRewardPunishCategory.getStatus());
            adminCategoryVo.setTitle(serviceRewardPunishCategory.getTitle());
            adminCategoryVo.setParentId(serviceRewardPunishCategory.getParentId());
            result.add(adminCategoryVo);
        }
        return result;
    }

    /**
     * 根据id,状态获取类目信息
     *
     * @param id     主键
     * @param status 状态
     * @return 查询结果
     */
    @Override
    public ServiceRewardPunishCategory getInfoByStatus(int id, int status) {
        QueryWrapper<ServiceRewardPunishCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("status",status);
        return this.getOne(queryWrapper);
    }

}
